package com.scalar.chiptrack.adaptor.amkor;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.XLSParserImpl;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.ParameterTypes;
import com.scalar.ScalarException;

import java.io.File;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Chandra Shekar.
 * Date: May 23, 2005
 * Time: 11:35:53 AM
 */
public class AMKORDataParser extends JobHandler implements AdaptorConstants
{
    private HashMap<String, AssemblyLot> m_atkAssyDataMap       = new HashMap<String, AssemblyLot>();
    private HashMap<String, AssemblyLot> m_atpAssyDataMap       = new HashMap<String, AssemblyLot>();

    private HashMap<String, InventoryLot> m_atkdieBankDataMap   = new HashMap<String, InventoryLot>();
    private HashMap<String, InventoryLot> m_atpdieBankDataMap   = new HashMap<String, InventoryLot>();

    private HashMap<String, ShippedLot> m_assyAtkShipDataMap    = new HashMap<String, ShippedLot>();
    private HashMap<String, ShippedLot> m_assyAtpShipDataMap    = new HashMap<String, ShippedLot>();

    private HashMap<String, TestLot> m_atptestDataMap           = new HashMap<String, TestLot>();
    private HashMap<String, TestLot> m_atktestDataMap           = new HashMap<String, TestLot>();

    private HashMap<String, ShippedLot> m_ftAtkShipDataMap    = new HashMap<String, ShippedLot>();
    private HashMap<String, ShippedLot> m_ftAtpShipDataMap    = new HashMap<String, ShippedLot>();

    private Logger m_parserLogger       = null;
    private String m_dataOutputDir      = null;

    public AMKORDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_AMKOR_ATK , WIP_TYPE_ASSEMBLY );

            if ( m_dataOutputDir != null )
            {
                AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );

                m_parserLogger= AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_AMKOR_ATK );

                AdaptorUtils.setLogger( m_parserLogger );
                ShippedLotDAO.setLogger( m_parserLogger);
                InventoryLotDAO.setLogger(m_parserLogger);
                TestAdaptorDAO.setLogger( m_parserLogger);
                AssemblyAdaptorDAO.setLogger( m_parserLogger );
            }
        }
        catch ( Exception se )
        {
            log( "ERROR!! ERROR!! ERROR!! Exception while loading AMKOR output directory details from database, Exception: " + se );
            System.out.println( "ERROR!! ERROR!! ERROR!! Exception while loading AMKOR output directory details from database, Exception: " + se );
        }
    }

    public static void main( String args[] ) throws Exception
    {
        try
        {
            new AMKORDataParser().execute();
        }
        catch ( Exception e )
        {
            throw e;
        }

    }

    public void execute() throws Exception
    {
        AdaptorUtils.printHeader( VENDOR_NAME_AMKOR_ATK, CONSTANT_PARSER );

        File doneDestDir = new File( m_dataOutputDir + "/done" );
        File dataoutDir  = new File( m_dataOutputDir );

        File[] fileList = dataoutDir.listFiles();

        if ( ( fileList == null ) || (fileList.length <= 3))
        {
            log("No file found in directory: " + dataoutDir);
            System.out.println("No file found in directory: " + dataoutDir );
            throw new ScalarException("No file found in directory: " + dataoutDir+ "----\n\n" );
        }

        ArrayList<File> successFileList = new ArrayList<File>();

        boolean success = false;

        for ( int i = 0; i < fileList.length; i++ )
        {
			if ( fileList[i].isDirectory() == true )
			{
			    continue;
			}

			try
            {
                // Step 1
                // Parse Files to get Object List
                if ( fileList[i].getName() != null && fileList[i].getName().startsWith( AMKORAdaptor.FILE_NAME_LOT_STATUS  ) )
                {
                    // To parse the the lot_status sheet in to table
                    parseLotStatusReport( fileList[i].getAbsolutePath() );

                    successFileList.add( fileList[i] );
                }
                success = true;
            }
            catch ( Exception e )
            {
                success = false;
                log( "Exception while parsing and storing file: " + fileList[i].getAbsolutePath() + "\nException: " + e );
                e.printStackTrace();
                throw e;
            }
        }
        try
        {
            if (m_atkdieBankDataMap.isEmpty() == false)
            {
                log(" Updating ATK Die Bank Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_atkdieBankDataMap.values().iterator(), LOCATION_TYPE_DIE_BANK, VENDOR_NAME_AMKOR_ATK);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_DIE_BANK, VENDOR_NAME_AMKOR_ATK);
            }
            success = true;
            m_atkdieBankDataMap.clear();
            m_atkdieBankDataMap = null;

            // Save Die Bank Information
            if (m_atpdieBankDataMap.isEmpty() == false)
            {
                log(" Updating ATP Die Bank Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_atpdieBankDataMap.values().iterator(), LOCATION_TYPE_DIE_BANK, VENDOR_NAME_AMKOR_ATP);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_DIE_BANK, VENDOR_NAME_AMKOR_ATP);
            }
            success = true;
            m_atpdieBankDataMap.clear();
            m_atpdieBankDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Die Bank Lot information into database: " + se);
            throw se;
        }

        /*Updating the Assembly Lot Information*/
        try
        {
            if (m_atkAssyDataMap.isEmpty() == false)
            {
                log(" Updating ATK Assembly Lot Information");
                AssemblyAdaptorDAO.saveAssemblyLot(null, m_atkAssyDataMap.values().iterator(),WIP_TYPE_ASSEMBLY, VENDOR_NAME_AMKOR_ATK);
            }
            else
            {
                AssemblyAdaptorDAO.closeAssemblyLot(null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_AMKOR_ATK);
            }

            m_atkAssyDataMap.clear();
            m_atkAssyDataMap = null;
            success = true;

            // Save Assembly Information
            if (m_atpAssyDataMap.isEmpty() == false)
            {
                log(" Updating ATP Assembly Lot Information");
                AssemblyAdaptorDAO.saveAssemblyLot(null, m_atpAssyDataMap.values().iterator(),WIP_TYPE_ASSEMBLY, VENDOR_NAME_AMKOR_ATP);
            }
            else
            {
                AssemblyAdaptorDAO.closeAssemblyLot(null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_AMKOR_ATP);
            }

            m_atpAssyDataMap.clear();
            m_atpAssyDataMap = null;
            success = true;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
            throw se;
        }
        try
        {
            if ( m_assyAtkShipDataMap.isEmpty() == false )
            {
                log( " Updating Assembly Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_assyAtkShipDataMap.values().iterator() );
            }
            success = true;
            m_assyAtkShipDataMap.clear();
            m_assyAtkShipDataMap = null;

            if ( m_assyAtpShipDataMap.isEmpty() == false )
            {
                log( " Updating Assembly Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_assyAtpShipDataMap.values().iterator() );
            }
            success = true;
            m_assyAtpShipDataMap.clear();
            m_assyAtpShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Shipping Lot information into database: " + se );
            throw se;
        }
		try
        {
           // Saving FINAL TEST Information
           if ( m_atktestDataMap.isEmpty() == false )
            {
                log( " Updating Test Lot Information" );
                TestAdaptorDAO.saveTestLot( null,m_atktestDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_AMKOR_ATK );
            }
            else
           {
               TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_AMKOR_ATK);
           }
           success = true;
           m_atktestDataMap.clear();
           m_atktestDataMap = null;
            // Saving FINAL TEST Information
           if ( m_atptestDataMap.isEmpty() == false )
            {
                log( " Updating Test Lot Information" );
                TestAdaptorDAO.saveTestLot( null,m_atptestDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_AMKOR_ATP );
            }
            else
           {
               TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_AMKOR_ATP);
           }
           success = true;
           m_atptestDataMap.clear();
           m_atptestDataMap = null; 
        }
        catch ( ScalarException se )
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se );
            throw se;
        }

        try
        {
            // Save FT Ship Information
            if ( m_ftAtkShipDataMap.isEmpty() == false )
            {
                log( " Updating FT Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_ftAtkShipDataMap.values().iterator() );
            }
            success = true;
            m_ftAtkShipDataMap.clear();
            m_ftAtkShipDataMap = null;

           // Save FT Ship Information
            if ( m_ftAtpShipDataMap.isEmpty() == false )
            {
                log( " Updating FT Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_ftAtpShipDataMap.values().iterator() );
            }
            success = true;
            m_ftAtpShipDataMap.clear();
            m_ftAtpShipDataMap = null; 
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update FT Shipping Lot information into database: " + se );
            throw se;
        }
        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_AMKOR_ATK,true, true, true );
        AdaptorDAO.sendAllNotifications( VENDOR_NAME_AMKOR_ATP,true, true, true );

        // To Close the Drop Out Lots
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_AMKOR_ATK );
        AdaptorDAO.closeDropOutLots( VENDOR_NAME_AMKOR_ATP );

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( AMKORAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);
        // transaction 9
        try
        {
            if (success)
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, AMKORAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println("SUCCESSFULLY UPDATED IN THE DATABASE");
            }
        }
        catch (ScalarException se)
        {
            success = false;  //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, AMKORAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log("WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se);
            throw se;
        }

        // Step 10
        // Move the successful files to 'done' directory
        if (success)
        {
            AdaptorUtils.moveFilesToDone( successFileList, doneDestDir);
        }
    } // end of execute method.


    private void parseLotStatusReport(String fileName) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result            = null;
        ShippedLot   shippedLot    = null;
        AssemblyLot  assemblyLot   = null;
        InventoryLot inventoryLot  = null;
        TestLot      testLot       = null;
        String partNumber       = null;
        String fabLotNumber     = null;
        String lotNumber        = null;
        String vendor			= null;
        boolean custPart = false;
        try
        {
            File detailWipFile = new File( fileName );
            if ( !detailWipFile.exists() )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Wip Report..." );

            Parser parser = ParserFactory.getXLSParser( detailWipFile );

            ((XLSParserImpl)parser).setHeaderRowIdx(1);
            ((XLSParserImpl)parser).setDataStartRowIdx(2);

            result = parser.parse();
            //System.out.println("result.getHeadings() = " + result.getHeadings());
            while ( result.next() )
            {
                 custPart = false;
                 partNumber = result.getString("Customer Target Device");
                 if (partNumber == null)
                 {
                     custPart = true;
                     partNumber = result.getString("Customer Source Device");
                 }
                 fabLotNumber = result.getString("Original Customer Lot Number");
                 lotNumber = result.getString("Factory Lot Number");

                if( result.getString("Current Plant") != null && result.getString("Current Plant").equalsIgnoreCase( "K1" ) )
                {
                	vendor = VENDOR_NAME_AMKOR_ATK;
                }
                else if ( result.getString("Current Plant") != null &&  result.getString("Current Plant").equalsIgnoreCase( "P3" ) )
                {
                	vendor = VENDOR_NAME_AMKOR_ATP;
                }
                if( lotNumber == null )
                {
                    lotNumber = fabLotNumber;
                }

                String lotStatus = null;
                String lotCategory = null;
                if (result.getString("Lot Handling Code") != null && "EL".equalsIgnoreCase(result.getString("Lot Handling Code") ) )
                {
                    lotCategory = LOT_CATEGORY_TYPE_ENGG;
                }
                else
                {
                    lotCategory = LOT_CATEGORY_TYPE_PROD;
                }
                if ( result.getString("Lot Hold Status Y/N") != null && "Y".equalsIgnoreCase(result.getString("Lot Hold Status Y/N") ) )
                {
                    lotStatus = LOT_STATUS_HOLD;
                }
                else
                {
                    lotStatus = LOT_STATUS_RUNNING;
                }

                if ( partNumber != null && partNumber.trim().length() > 0 && lotNumber != null && lotNumber.trim().length() >0 && vendor != null )
                {
                    int index = partNumber.indexOf("-");
                    if ( index > 0 && !custPart)
                    {
                        partNumber = partNumber.substring(0, index);
                    }

                     if ( result.getString("Business Code") != null && "A".equalsIgnoreCase(result.getString("Business Code") ) )
                     {
                         if ( result.getString("Current Operation Full Description") != null
                                 && "DIE BANK".equalsIgnoreCase(result.getString("Current Operation Full Description")) )
                         {
                             if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATP ) )
                             {
                                 inventoryLot = (InventoryLot)m_atpdieBankDataMap.get( partNumber + fabLotNumber );

                                 if ( inventoryLot == null )
                                 {
                                    inventoryLot = new InventoryLot( VENDOR_NAME_AMKOR_ATP, LOCATION_TYPE_DIE_BANK );
                                    m_atpdieBankDataMap.put( partNumber + fabLotNumber, inventoryLot );
                                 }
                             }
                             else if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATK ) )
                             {
                                 inventoryLot = (InventoryLot)m_atkdieBankDataMap.get( partNumber + fabLotNumber );

                                 if ( inventoryLot == null )
                                 {
                                    inventoryLot = new InventoryLot( VENDOR_NAME_AMKOR_ATK, LOCATION_TYPE_DIE_BANK );
                                    m_atkdieBankDataMap.put( partNumber + fabLotNumber, inventoryLot );
                                 }
                             }

                             int quantity = 0;
                             int wquantity = 0;
                             if (inventoryLot.getQtyIn() != null)
                             {
                                try
                                {
                                    quantity = Integer.parseInt(inventoryLot.getQtyIn());
                                    wquantity = Integer.parseInt(inventoryLot.getWaferQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }

                                //if ( result.getInt("Die qty") > 0 )
                                //{
                                    quantity += result.getInt("Lot Current Qty");
                                //}
                                /*else
                                {
                                    int dbProcessStepId = NewAdaptorDAO.getProcessStepId( partNumber, LOCATION_TYPE_DIE_BANK, LOCATION_TYPE_DIE_BANK );
                                    int gdpw = 0;
                                    try
                                    {   int fabProcessStepId = NewProductDAO.getForwardOrBackwardProcessstepId(null, dbProcessStepId,0, WIP_TYPE_FAB );
                                        gdpw = Integer.parseInt(NewProductDAO.getProcessParameterValue( null, fabProcessStepId, ParameterTypes.grossDieWafer ) ) ;
                                    }
                                    catch( NumberFormatException nfe ) { gdpw = 0; }

                                    quantity +=  ( waferIds.split(";").length ) * gdpw;
							    }*/
                            }
                            else
                            {
                                //if ( result.getInt("Die qty") > 0 )
                                //{
                                    quantity = result.getInt("Lot Current Qty");
                                /*}
                                else
                                {
                                    int dbProcessStepId = NewAdaptorDAO.getProcessStepId( partNumber, LOCATION_TYPE_DIE_BANK, LOCATION_TYPE_DIE_BANK );
                                    int gdpw = 0;
                                    try
                                    {
                                        int fabProcessStepId = NewProductDAO.getForwardOrBackwardProcessstepId(null, dbProcessStepId,0, WIP_TYPE_FAB );
                                        gdpw = Integer.parseInt(NewProductDAO.getProcessParameterValue( null, fabProcessStepId, ParameterTypes.grossDieWafer ) ) ;
                                    }
                                    catch( NumberFormatException nfe ) { gdpw = 0; }
									quantity = wquantity * gdpw;
								}*/
                            }

                            if (quantity > 0 )
                            {
                                inventoryLot.setPartNumber( partNumber );
                                inventoryLot.setLotNumber( fabLotNumber );
                                inventoryLot.setQtyIn(quantity + "");
                                inventoryLot.setWaferQtyIn( wquantity + "");
                                if (result.getDate("Receipt Date") != null && result.getString("Receipt Date").trim().length() > 0 )
                                {
                                    inventoryLot.setDateIn( StringUtils.formatDate( result.getString("Receipt Date"), "yyyyMMdd","MM/dd/yyyy" ) );
                                }
                                inventoryLot.setFabLotNumber( fabLotNumber );
                                inventoryLot.setStatus( lotStatus );
                                inventoryLot.setLotCategory( lotCategory );
                            }
                         }//end of Die ABnk
                         //ASSY
                         if ( result.getString("Current Operation Full Description") != null
                                 && !"SHIPPED".equalsIgnoreCase(result.getString("Current Operation Full Description"))
                                 && !"DIE BANK".equalsIgnoreCase(result.getString("Current Operation Full Description"))
                                 && !"TRANSFER".equalsIgnoreCase(result.getString("Current Operation Full Description")) )
                         {
                            if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATP ) )
                            {
                                assemblyLot = (AssemblyLot) m_atpAssyDataMap.get( partNumber + lotNumber + fabLotNumber );
                                if (assemblyLot == null)
                                {
                                    assemblyLot = new AssemblyLot( VENDOR_NAME_AMKOR_ATP, WIP_TYPE_ASSEMBLY );
                                    m_atpAssyDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                                }
                            }
                            else if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATK ) )
                            {
                                assemblyLot = (AssemblyLot) m_atkAssyDataMap.get( partNumber + lotNumber + fabLotNumber );
                                if (assemblyLot == null)
                                {
                                    assemblyLot = new AssemblyLot( VENDOR_NAME_AMKOR_ATK, WIP_TYPE_ASSEMBLY );
                                    m_atkAssyDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                                }
                            }
                            int quantity = 0;
                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    quantity = Integer.parseInt(assemblyLot.getQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }
                                quantity += result.getInt("Lot Current Qty");
                            }
                            else
                            {
                                quantity = result.getInt("Lot Current Qty");
                            }

                            if ( quantity > 0 )
                            {
                                assemblyLot.setLotNumber( lotNumber );
                                assemblyLot.setDeviceName( partNumber );
                                assemblyLot.setStatus(lotStatus);
                                assemblyLot.setFabLotNumber( fabLotNumber );
                                assemblyLot.setCategory( lotCategory );
                                assemblyLot.setDateIn( StringUtils.formatDate(result.getString("Assembly Start Date"), "yyyyMMdd", "MM/dd/yyyy") );
                                assemblyLot.setExpectedDateOut( StringUtils.formatDate(result.getString("Proj Assy Out Date"), "yyyyMMdd", "MM/dd/yyyy") );
                                assemblyLot.setStage( result.getString("Current Operation Full Description"));
                                assemblyLot.setQtyIn( quantity+"" );
                                assemblyLot.setPONumber(result.getString("Customer P.O. Number"));
                            }
                         }
                         //ASSY SHIP
                         if ( result.getString("Current Operation Full Description") != null
                                 && "SHIPPED".equalsIgnoreCase(result.getString("Current Operation Full Description")) )
                         {
                               String shipDate = null;
                               if ( result.getString("Actual Ship Date") != null )
                               {
                                    shipDate = StringUtils.formatDate(result.getString("Actual Ship Date"), "yyyyMMdd", "MM/dd/yyyy");
                               }        
                               if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATP ) )
                               {
                                   shippedLot = ( ShippedLot ) m_assyAtpShipDataMap.get ( partNumber + lotNumber + fabLotNumber + shipDate );

                                   if ( shippedLot == null )
                                   {
                                        shippedLot = new ShippedLot(VENDOR_NAME_AMKOR_ATP, LOCATION_TYPE_ASSEMBLY);
                                        m_assyAtpShipDataMap.put( partNumber + lotNumber + fabLotNumber + shipDate, shippedLot );
                                   }
                               }
                               else if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATK ) )
                               {
                                   shippedLot = ( ShippedLot ) m_assyAtkShipDataMap.get ( partNumber + lotNumber + fabLotNumber +  shipDate );

                                   if ( shippedLot == null )
                                   {
                                        shippedLot = new ShippedLot(VENDOR_NAME_AMKOR_ATK, LOCATION_TYPE_ASSEMBLY);
                                        m_assyAtkShipDataMap.put( partNumber + lotNumber + fabLotNumber +  shipDate, shippedLot );
                                   }
                               }

                               int quantity = 0;
                               if ( shippedLot.getShipQty() != null )
                               {
                                   try
                                   {
                                       quantity = Integer.parseInt( shippedLot.getShipQty() );
                                   }
                                   catch ( NumberFormatException ignore )
                                   {
                                       quantity = 0;
                                   }
                                   quantity += result.getInt("Ship Qty");
                               }
                               else
                               {
                                   quantity = result.getInt("Ship Qty");
                               }

                               if ( quantity > 0 )
                               {
                                   shippedLot.setLotNumber( lotNumber );
                                   shippedLot.setPartNumber( partNumber );
                                   shippedLot.setShipDate( shipDate );
                                   shippedLot.setShipQty( quantity + "" );
                                   //shippedLot.setNextLocation( nextLocation );
                                   shippedLot.setInvoiceNumber( result.getString("Commercial Invoice") );
                                   shippedLot.setFabLotNumber( fabLotNumber );
                                   shippedLot.setProcessType( LOCATION_TYPE_ASSEMBLY );
                               }
                         }
                     }
                     // TEST PLANT
                     if ( result.getString("Business Code") != null && "T".equalsIgnoreCase(result.getString("Business Code") ) )
                     {
                         if ( result.getString("Current Operation Full Description") != null
                                 && !"SHIPPED".equalsIgnoreCase(result.getString("Current Operation Full Description")) )
                         {
                             if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATP ) )
                             {
                                 testLot = (TestLot)m_atptestDataMap.get( partNumber + lotNumber + fabLotNumber );

                                 if ( testLot == null )
                                 {
                                     testLot = new TestLot( VENDOR_NAME_AMKOR_ATP, LOCATION_TYPE_FT );
                                     m_atptestDataMap.put( partNumber + lotNumber + fabLotNumber, testLot );
                                 }
                             }
                             else if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATK ) )
                             {
                                 testLot = (TestLot)m_atktestDataMap.get( partNumber + lotNumber + fabLotNumber );

                                 if ( testLot == null )
                                 {
                                     testLot = new TestLot( VENDOR_NAME_AMKOR_ATK, LOCATION_TYPE_FT );
                                     m_atktestDataMap.put( partNumber + lotNumber + fabLotNumber, testLot );
                                 }
                             }
                             int quantity = 0;
                             if (testLot.getQtyIn() != null)
                             {
                                try
                                {
                                    quantity = Integer.parseInt(testLot.getQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }
                                quantity += result.getInt("Lot Current Qty");
                             }
                             else
                             {
                                quantity = result.getInt("Lot Current Qty");
                             }

                             if ( quantity > 0 )
                             {
                                testLot.setPartNumber( partNumber );
                                testLot.setLotNumber( lotNumber );
                                testLot.setFabLotNumber( fabLotNumber );
                                testLot.setDateIn( StringUtils.formatDate(result.getString("Test Start Date"),"yyyyMMdd","MM/dd/yyyy") );
                                if ( result.getString("Proj Test Out Date") != null && result.getString("Proj Test Out Date").trim().length() > 7 )
                                {
                                    testLot.setExpecetedDate(StringUtils.formatDate(result.getString("Proj Test Out Date"),"yyyyMMdd","MM/dd/yyyy"));
                                }
                                testLot.setStage( result.getString("Current Operation Full Description") );
                                testLot.setQtyIn( quantity + "");
                                testLot.setStatus(lotStatus);
                                testLot.setCategory( lotCategory );
                                testLot.setPoNumber(result.getString("Customer P.O. Number"));
                             }
                         } // END OF FT
                         
                         //FT SHIP
                         if ( result.getString("Current Operation Full Description") != null
                                 && "SHIPPED".equalsIgnoreCase(result.getString("Current Operation Full Description")) )
                         {
                               String shipDate = null;
                               if ( result.getString("Actual Ship Date") != null )
                               {
                                    shipDate = StringUtils.formatDate(result.getString("Actual Ship Date"), "yyyyMMdd", "MM/dd/yyyy");
                               }
                               if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATP ) )
                               {
                                   shippedLot = ( ShippedLot ) m_ftAtpShipDataMap.get ( partNumber + lotNumber + fabLotNumber + shipDate );

                                   if ( shippedLot == null )
                                   {
                                        shippedLot = new ShippedLot(VENDOR_NAME_AMKOR_ATP, LOCATION_TYPE_TEST);
                                        m_ftAtpShipDataMap.put( partNumber + lotNumber + fabLotNumber + shipDate, shippedLot );
                                   }
                               }
                               else if ( vendor!= null && vendor.equalsIgnoreCase( VENDOR_NAME_AMKOR_ATK ) )
                               {
                                   shippedLot = ( ShippedLot ) m_ftAtkShipDataMap.get ( partNumber + lotNumber + fabLotNumber +  shipDate );

                                   if ( shippedLot == null )
                                   {
                                        shippedLot = new ShippedLot(VENDOR_NAME_AMKOR_ATK, LOCATION_TYPE_TEST);
                                        m_ftAtkShipDataMap.put( partNumber + lotNumber + fabLotNumber +  shipDate, shippedLot );
                                   }
                               }

                               int quantity = 0;
                               if ( shippedLot.getShipQty() != null )
                               {
                                   try
                                   {
                                       quantity = Integer.parseInt( shippedLot.getShipQty() );
                                   }
                                   catch ( NumberFormatException ignore )
                                   {
                                       quantity = 0;
                                   }
                                   quantity += result.getInt("Ship Qty");
                               }
                               else
                               {
                                   quantity = result.getInt("Ship Qty");
                               }

                               if ( quantity > 0 )
                               {
                                   shippedLot.setLotNumber( lotNumber );
                                   shippedLot.setPartNumber( partNumber );
                                   shippedLot.setShipDate( shipDate );
                                   shippedLot.setShipQty( quantity + "" );
                                   //shippedLot.setNextLocation( nextLocation );
                                   shippedLot.setInvoiceNumber( result.getString("Commercial Invoice") );
                                   shippedLot.setFabLotNumber( fabLotNumber );
                                   shippedLot.setProcessType( LOCATION_TYPE_FT );
                               }

                         }
                     } //END TEST BLOCK
                }
            }
        }
        catch (Exception e)
        {

        }
    }

   private void log( String message )
   {
       if ( message == null ) return;

       if ( m_parserLogger != null )
       {
           m_parserLogger.logDebug( message + LINE_SEPARATOR );
       }
       else
       {
           System.out.println( message );
       }
   } // end of log method

} // end of class

